/*
 * Title:        CloudScheduler Toolkit
 * Description:  a Toolkit for Modeling and Simulation of Job Scheduling and Resource Provisioning in Cloud System
 * Licence:      GPL - http://www.gnu.org/copyleft/gpl.html
 * Author:       Fuhui Wu
 *
 * Copyright (c) 2013-2014, The National University of Defense Technology, China
 */

package org.nudt.jCloud.example.resourceStealing1;

import org.nudt.jCloud.evaluation.Evaluation;
import org.nudt.jCloud.infrastructure.HomegeneousCluster;
import org.nudt.jCloud.infrastructure.SlotType;
import org.nudt.jCloud.jobScheduler.AFCFSGangScheduler;
import org.nudt.jCloud.jobScheduler.AFCFSScheduler;
import org.nudt.jCloud.jobScheduler.JobScheduler;
import org.nudt.jCloud.resourceManager.GeneralReservationRM;
import org.nudt.jCloud.resourceManager.GeneralShareStateRM;
import org.nudt.jCloud.resourceManager.ResManager;
import org.nudt.jCloud.resourceScaler.ConstantScaler;
import org.nudt.jCloud.resourceScaler.ResourceScaler;
import org.nudt.jCloud.simulation.CloudSchedSIM;
import org.nudt.jCloud.simulation.SIMTag;
import org.nudt.jCloud.simulation.User;
import org.nudt.jCloud.simulation.UserBroker;
import org.nudt.jCloud.workload.BatchWLGen;
import org.nudt.jCloud.workload.GangWLGen;
import org.nudt.jCloud.workload.Workload;
import org.nudt.jCloud.workload.WorkloadGenerator;

public class GangBatch {
	
	private static SlotType slotType;
	private static HomegeneousCluster homeCluster;
	private static ResManager resManager;
	private static UserBroker userBroker1;
	private static ResourceScaler resourceScaler1;
	private static JobScheduler jobScheduler1;
	private static Workload wl1;
	private static User user1;
	private static UserBroker userBroker2;
	private static ResourceScaler resourceScaler2;
	private static JobScheduler jobScheduler2;
	private static Workload wl2;
	private static User user2;
	
	public static void init(){
		slotType = new SlotType(1, 1000, 250, 0);
		homeCluster = new HomegeneousCluster(30, 1000, 4, 4000, 1000, 200000);
		resManager = new GeneralReservationRM("ReservationRM", homeCluster);
		userBroker1 = new UserBroker("UserBroker1", SIMTag.RM_MODE_R);
		jobScheduler1 = new AFCFSGangScheduler("AFCFS_Gang");
		resourceScaler1 = new ConstantScaler(slotType, 80);
		WorkloadGenerator wlGen1 = new GangWLGen(100, 40, 80, 0.5, 60, slotType);
		wl1 = wlGen1.generate(0);
		user1 = new User("user1", wl1);
		userBroker1.addUser(user1);
		
		userBroker2 = new UserBroker("UserBroker2", SIMTag.RM_MODE_R);
		resourceScaler2 = new ConstantScaler(slotType, 40);
		jobScheduler2 = new AFCFSScheduler("AFCFS");
		WorkloadGenerator wlGen2 = new BatchWLGen(100, 40, 80, 0.5, 30, slotType);
		wl2 = wlGen2.generate(0);
		user2 = new User("user2", wl2);
		userBroker2.addUser(user2);
	}
	
	public static void reset(String simName){
		CloudSchedSIM.reset(simName);
		homeCluster.reset();
		userBroker1.reset();
		userBroker2.reset();
		user1.reset();
		user2.reset();
		userBroker1.addUser(user1);
		userBroker2.addUser(user2);
		jobScheduler1.reset();
		jobScheduler2.reset();
		resourceScaler1.reset();
		resourceScaler2.reset();
	}
	
	public static void start(){
		CloudSchedSIM.addRM(resManager);
		CloudSchedSIM.addUserBroker(userBroker1);
		CloudSchedSIM.addUserBroker(userBroker2);
		CloudSchedSIM.startSimulation();		
	}
	
	public static void main(String[] args) {
		init();
		CloudSchedSIM.init("GangBatch");
		userBroker1.init(jobScheduler1, resourceScaler1);
		userBroker2.init(jobScheduler2, resourceScaler2);
		start();
		
		System.out.println("------------------------------------------------------");
		
		reset("ShareState");
		resManager = new GeneralShareStateRM("ShareState", homeCluster);
		jobScheduler1 = new AFCFSGangScheduler("AFCFS_Gang");
		resourceScaler1 = new ConstantScaler(slotType, 60);
		resourceScaler2 = new ConstantScaler(slotType, 60);
		jobScheduler2 = new AFCFSScheduler("AFCFS");
		userBroker1.init(jobScheduler1, resourceScaler1);
		userBroker2.init(jobScheduler2, resourceScaler2);
		userBroker1.setRM_Mode(SIMTag.RM_MODE_S);
		userBroker2.setRM_Mode(SIMTag.RM_MODE_S);
		start();
		
		System.out.println("------------------------------------------------------");
		
		reset("Dynamic_R");
		resManager = new GeneralReservationRM("ShareState", homeCluster);
		jobScheduler1 = new AFCFSGangScheduler("AFCFS_Gang");
		resourceScaler1 = new ConstantScaler(slotType, 90);
		resourceScaler2 = new ConstantScaler(slotType, 30);
		jobScheduler2 = new AFCFSScheduler("AFCFS");
		userBroker1.init(jobScheduler1, resourceScaler1);
		userBroker2.init(jobScheduler2, resourceScaler2);
		userBroker1.setRM_Mode(SIMTag.RM_MODE_R);
		userBroker2.setRM_Mode(SIMTag.RM_MODE_R);
		start();
		
		Evaluation.avgWaitTime("GangBatch", "user1");
		Evaluation.avgWeightedWaitTime("GangBatch", "user1");
		Evaluation.avgWaitTime("GangBatch", "user2");
		Evaluation.avgWeightedWaitTime("GangBatch", "user2");
		System.out.println();
		Evaluation.avgWaitTime("ShareState", "user1");
		Evaluation.avgWeightedWaitTime("ShareState", "user1");
		Evaluation.avgWaitTime("ShareState", "user2");
		Evaluation.avgWeightedWaitTime("ShareState", "user2");
		System.out.println();
		Evaluation.avgWaitTime("Dynamic_R", "user1");
		Evaluation.avgWeightedWaitTime("Dynamic_R", "user1");
		Evaluation.avgWaitTime("Dynamic_R", "user2");
		Evaluation.avgWeightedWaitTime("Dynamic_R", "user2");
	}
	
	
	public ResManager getResManager() {
		return resManager;
	}
}
